/*
 * @Date: 2021-12-12 19:47:04
 * @LastEditors: LeeKing
 * @LastEditTime: 2021-12-16 14:27:50
 * @FilePath: \admin\src\utils\request.js
 */
// 导出一个axios的实例  而且这个实例要有请求拦截器 响应拦截器
import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'

const request = axios.create({
  baseURL: 'https://www.liulongbin.top:8888/api/private/v1'
}) // 创建一个axios的实例
request.interceptors.request.use((config) => {
  if (store.state.user.token) {
    config.headers.Authorization = store.state.user.token
  }
  return config
}) // 响应拦截器

request.interceptors.response.use(
  (res) => {
    const { status, data } = res

    if (status === 200) {
      return data
    } else {
      Message.error(data.meta.msg)
      return Promise.reject(new Error(data.meta.msg))
    }
  },
  (err) => {
    Message.error('错误了')
    console.log(err)
    return Promise.reject(err)
  }
) // 响应

export default request // 导出axios实例
